<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class User extends Controller
{
    public function index()
    {
//        // 原生SQL
//        $list = Db::query('SELECT * FROM user');
//        $list = Db::query('SELECT * FROM user WHERE id=?', [1]);
//        // DB类
////        $list = Db::table('user')->select();
//        $list = Db::name('user')->order(['id'=>'asc'])->select();
        // 助手函数
        $list = db('lover')->order(['id' => 'asc'])->select();
        dump($list);
    }

    public function insert()
    {
        // 原生SQL
//        $result = Db::execute('INSERT INTO `user` (`name`, `age`) VALUES ("kk1", "82")');
//        $result = Db::execute('INSERT INTO `user` (`name`, `age`) VALUES (:name, :age)', ['name'=>'kk2', 'age'=>'85']);

        $data = [
            'name' => 'kk88',
            'age' => '89'
        ];
        // DB类
//        $result = Db::table('user')->insertGetId($data);
//        $result = Db::table('user')->data($data)->insert();
        // 助手函数
        $result = db('user')->insert($data);
        dump($result);
    }

    public function delete()
    {
        // 原生SQL
//        $result = Db::execute('DELETE FROM user WHERE name="kk2"');

        // DB类
//        $result = Db::table('user')->delete(54);
//        $result = Db::table('user')->where('name','kk1')->delete();
        // 助手函数
        $result = db()->table('user')->delete(57);
        dump($result);
    }

    public function update()
    {
        // 原生SQL
//        $result = Db::execute('UPDATE user SET age="40" WHERE id="1"');
        // DB类
//        $result = Db::table('user')->where('id',1)->update(['age'=>50]);
        // 助手函数
        $result = db()->table('user')->where('id',1)->update(['age'=>18]);
        dump($result);
    }

}
